package com.android.mangalib.activity;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.SimpleFormatter;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPathExpressionException;

import org.w3c.dom.DOMException;
import org.xml.sax.SAXException;

import com.android.mangalib.constant.Constant;
import com.android.mangalib.parser.XMLParser;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

/**
 * This activity is called when the user click on the "add button" in the collection view
 */
public class AddTomeActivity extends Activity {

	CheckBox addOwnedCheckBox;
	TextView addTitleTextView;
	EditText addTitleEditText;
	TextView addDateTextView;
	EditText addDateEditText;
	TextView addPriceTextview;
	EditText addPriceEditText;
	Button addButtonValid;
	
    @Override
    /**
	 * Main method, called when the activity is created
	 */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_tome);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        
        // Disable the "previous" button and remove the logo 
        getActionBar().setDisplayHomeAsUpEnabled(false);
        getActionBar().setDisplayShowHomeEnabled(false);
        
        addOwnedCheckBox = (CheckBox)findViewById(R.id.addOwnedCheckBox);
    	addTitleEditText = (EditText)findViewById(R.id.addTitleEditText);
    	addDateEditText = (EditText)findViewById(R.id.addDateEditText);
    	addPriceEditText = (EditText)findViewById(R.id.addPriceEditText);
    	addButtonValid = (Button)findViewById(R.id.addButtonValid);
        
        // Get the title of the collection
        final String title = getIntent().getStringExtra(Constant.ADD_TOME_ACTIVITY);
        
        // Set the title of the activity as the title of the collection
        setTitle(title);
        
        addOwnedCheckBox.setClickable(true);
        addOwnedCheckBox.setText("Owned");
        addButtonValid.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try {
					XMLParser.addTome(getApplicationContext(), addTitleEditText.getText().toString(), addPriceEditText.getText().toString(), String.valueOf(addOwnedCheckBox.isChecked()), addDateEditText.getText().toString(), title);				
					if (!addOwnedCheckBox.isChecked()) {
						Calendar cal = Calendar.getInstance();
						SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
						simpleDateFormat.applyPattern(Constant.FORMAT_DATE);
						cal.setTime(simpleDateFormat.parse(addDateEditText.getText().toString()));
						cal.set(Calendar.HOUR, 15);
						Intent intent = new Intent(Intent.ACTION_EDIT);
						intent.setType("vnd.android.cursor.item/event");
						intent.putExtra("beginTime", cal.getTimeInMillis());
						intent.putExtra("allDay", false);
						intent.putExtra("endTime", cal.getTimeInMillis() + 60*60*1000);
						intent.putExtra("title", title + " " + addTitleEditText.getText().toString() + " available tomorrow !");
						startActivity(intent);
					}
					

				} catch (Exception e) {
					Log.v("ERROR", e.getMessage());
		//			e.printStackTrace();
				}
			}
		});
        
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_add_tome, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
